package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.ProblemDetail;
import com.baidumap.administrator.happystudy.utils.BeanUtils;
import com.baidumap.administrator.happystudy.utils.GlideUtil;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by C on 2017/3/25.
 */
public class SubjectDetailActivity extends AppBaseActivity implements View.OnClickListener, OnMessageResponse {


    @Bind(R.id.tv_shu_xue)
    TextView tvShuXue;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.tv_school)
    TextView tvSchool;
    @Bind(R.id.pro_img_01)
    ImageView proImg01;
    @Bind(R.id.pro_img_02)
    ImageView proImg02;
    @Bind(R.id.pro_img_03)
    ImageView proImg03;
    @Bind(R.id.tec_img)
    ImageView tecImg;
    @Bind(R.id.tec_name)
    TextView tecName;
    @Bind(R.id.tec_school)
    TextView tecSchool;
    @Bind(R.id.tv_rank)
    TextView tvRank;
    @Bind(R.id.tv_solved_num)
    TextView tvSolvedNum;
    @Bind(R.id.tv_ti_mu)
    TextView tvTiMu;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_jie_xi)
    TextView tvJieXi;
    @Bind(R.id.img_jiexi_001)
    ImageView imgJiexi001;
    @Bind(R.id.img_jiexi_002)
    ImageView imgJiexi002;
    @Bind(R.id.img_jiexi_003)
    ImageView imgJiexi003;
    @Bind(R.id.img_jiexi_004)
    ImageView imgJiexi004;
    private String mTiMuId;

    @Bind(R.id.layout_answer_empty)
    LinearLayout layout_answer_empty;
    @Bind(R.id.ll_teacher_answer)
    LinearLayout ll_teacher_answer;

    private SimpleDraweeView dvWelcome;
    DraweeController draweeController;

    @Override
    public int bindLayout() {
        return R.layout.activity_subject_detail;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        Fresco.initialize(this);
        LinearLayout image = (LinearLayout) findViewById(R.id.image);
        LinearLayout image1 = (LinearLayout) findViewById(R.id.image1);
        image.setOnClickListener(this);
        image1.setOnClickListener(this);
        getBundleValue();


        /**
         * 下面是主要代码：
         */
        draweeController = Fresco.newDraweeControllerBuilder()
                .setAutoPlayAnimations(true)//自动播放动画
//                .setUri(Uri.parse("http://s1.dwstatic.com/group1/M00/9B/31/468f0fc71e49479bf021c64cefee2882.gif"))//路径
                .setUri(Uri.parse("asset://com.baidumap.administrator.happystudy/teacher_business.gif"))//路径
                .build();

        dvWelcome = (SimpleDraweeView) view.findViewById(R.id.dv_welcome);

    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.ti_mu_xiang_qing);
        LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
        Api.quizDetail(this, sHelper.getString(Constant.TOKEN), sHelper.getString(Constant.UID), mTiMuId);
    }

    /**
     * 获取bundleValue
     */
    private void getBundleValue() {
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {

            mTiMuId = bundle.getString("timu_id");
        }
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.image1:
//                intent = new Intent(SubjectDetailActivity.this, ImageActivity.class);
//                startActivity(intent);
                break;
            case R.id.image:
//                intent = new Intent(SubjectDetailActivity.this, ImageActivity.class);
//                startActivity(intent);
                break;
        }
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.QUIZ_DETAIL) && jo != null) {
            ProblemDetail problemDetail = new ProblemDetail(jo.toString());

            if (problemDetail != null) {


                if (problemDetail.getCode().equals("200")) {

                    tvShuXue.setText("编号：" + problemDetail.getData().getId());
                    tvTitle.setText(problemDetail.getData().getTitle());
                    tvContent.setText(problemDetail.getData().getContent());
                    tvTime.setText(AbStrUtil.stampToDate(problemDetail.getData().getCreated_at()));
                    tvSchool.setText(problemDetail.getData().getSchool() + " " + problemDetail.getData().getSection() + " " + problemDetail.getData().getGrade());
                    GlideUtil.displayImg(this, problemDetail.getData().getImages().get(0), proImg01);


                    try {
                        if (BeanUtils.checkObjFieldIsNull(problemDetail.getData().getTeacher())) {
                            layout_answer_empty.setVisibility(View.VISIBLE);
                            ll_teacher_answer.setVisibility(View.GONE);
                            dvWelcome.setController(draweeController);
                        } else {
                            layout_answer_empty.setVisibility(View.GONE);
                            ll_teacher_answer.setVisibility(View.VISIBLE);

                            GlideUtil.displayImg(this, Constant.DOMAIN + problemDetail.getData().getTeacher().getTeacher_pic(), tecImg);
                            if (problemDetail.getData().getImages().size() > 0) {
                                GlideUtil.displayImg(this, problemDetail.getData().getAnswer_images().get(0), imgJiexi001);

                            }
                            if (problemDetail.getData().getImages().size() > 1) {
                                GlideUtil.displayImg(this, problemDetail.getData().getAnswer_images().get(1), imgJiexi002);

                            }
                            if (problemDetail.getData().getImages().size() > 2) {
                                GlideUtil.displayImg(this, problemDetail.getData().getAnswer_images().get(2), imgJiexi003);

                            }
                            if (problemDetail.getData().getImages().size() > 3) {
                                GlideUtil.displayImg(this, problemDetail.getData().getAnswer_images().get(3), imgJiexi004);

                            }

                            tecName.setText(problemDetail.getData().getTeacher().getName());
                            tvRank.setText("排名" + problemDetail.getData().getTeacher().getRank());
                            tvSolvedNum.setText("已解答" + problemDetail.getData().getTeacher().getQuestions() + "道");
                            tvTiMu.setText(problemDetail.getData().getTitle());
                            tvJieXi.setText(problemDetail.getData().getAnswer_content());
                            tecSchool.setText(problemDetail.getData().getTeacher().getSchool_name() + "  " + problemDetail.getData().getTeacher().getSection());

                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastUtil.showMessage(this, problemDetail.getMsg());
                }
            }

        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
